<?php
namespace Home\Controller;
use Think\Controller;
class MsgController extends ExtendController{
	
	public function msg(){
        $this->_cmd('_msg_');
    }
    //从数据库得到主题的所有列表	
    protected function _msg_list(){
        $msg=D('msg');
        $start=I('post.start',0,'intval');
        $limit=I('post.limit',20,'intval');
        $condition=array();
        $userName=I('post.userName',false,'trim');
        if($userName){
        	$condition[]="msg.name='$userName'";
        }
        $beginTime=strtotime(I('post.beginTime',false,'trim'));
        $endTime=strtotime(I('post.endTime',false,'trim'));
        if($beginTime){
        	$condition[]="msg.update_time>='$beginTime'";
        	if($endTime){
        		$condition[]="msg.update_time<='$endTime'";
        	}
        }
        $replayUser=I('post.replayUser',false,'trim');
        if($replayUser){
        	$condition[]="replay.name='$replayUser'";
        }
        $condition=implode(' and ',$condition);//数组转变为字符串  msg.name='admin' and msg.update_time>='1433088000' and msg.update_time<='1436457600' and replay.name='admin'
        $result=$msg->field("msg.*")->join('left join replay on msg.msg_id = replay.msg_id')->where($condition)->limit($start,$limit)->order("msg.update_time desc")->select();
        $count=$msg->join('left join replay on msg.msg_id = replay.msg_id')->where($condition)->limit($start,$limit)->count();
        $this->ajaxRespond(array('data'=>$result,'total'=>$count));
    }
    //SELECT * FROM `msg` left join replay ON msg.msg_id = replay.msg_id  WHERE ( msg.name='ellen' and replay.name='admin' and msg.update_time>='1436803200' and msg.update_time<='1436803200' ) LIMIT 0,20  
    //field("msg.*")选取msg所有列
    
    //删除主题
    protected function _msg_delMsg(){
    	$userName=I('get.userName');
    	if($userName!="admin"){
    		$this->ajaxRespond(array('success'=>false));
    	}
    	$msgId=I('get.msgId');
    	$msg=D('msg');
    	$replay=D('replay');
    	$result=$msg->where("msg_id='$msgId'")->delete();
    	$tmp=$replay->where("msg_id='$msgId'")->delete();
    	if(!empty($result)){
    		$this->ajaxRespond(array('success'=>true));
    	}
    	$this->ajaxRespond(array('success'=>false));
    }

    //添加留言
    protected function _msg_addMsg(){
    	$data = i('post.',array());
   		$data['updateTime']=time();
    	$tmp=array(
    		'content'=>$data['content'],
    		'title'=>$data['title'],
    		'name'=>$data['name'],
    		'update_time'=>$data['updateTime']	
    	);
    	$msg=D('msg');
    	$result=$msg->add($tmp);
    	if(!empty($result)){
    		$this->ajaxRespond(array('success'=>1));
    		exit;
    	}
    	$this->ajaxRespond(array('success'=>false,"msg"=>2222));
    }
    
    //删除按钮
    protected  function  _msg_delButton(){
    	$userName=I('get.userName');
    	if($userName!="admin"){
    		$this->ajaxRespond(array('success'=>false));
    	}
    	$msgIds=I('get.msgIds');
    	$msgIds=explode(",",$msgIds);
    	$map=array();
    	$map['msg_id']= array('IN',$msgIds);
    	$result=D('msg')->where($map)->delete();
    	$tep=D('replay')->where($map)->delete();
    }
    
    //回复主题
    protected function _msg_replayMsg(){
    	$data=i('post.',array());
    	$data['name']='admin';
    	$data['updateTime']=time();
    	$tmp=array(
    		'content'=>$data['content'],
    		'msg_id'=>$data['msgId'],
    		'name'=>$data['name'],
    		'update_time'=>$data['updateTime']
    	);
    	$result=D('replay')->add($tmp);
    	if(!empty($result)){
    		$this->ajaxRespond(array('success'=>1));
    		exit;
    	}
    	$this->ajaxRespond(array('success'=>false,"msg"=>2222));
    }    
    
    //查看某主题下的所有回复
    protected function _msg_seeReplay(){
    	$msgId=I("get.msgId",0,'intval');
    	if($msgId==0){
    		exit;
    	}
    	$result=D('replay')->where("msg_id='$msgId'")->order('update_time desc')->select();
    	$this->ajaxRespond(array('data'=>$result));
    }
    
    //删除某主题下的回复
    protected function _msg_delReplay(){
    	$replayId=I("get.replayId",0,'intval');
    	if($replayId==0){
    		exit;
    	}
    	$result=D('replay')->where("id='$replayId'")->delete();
    	$this->ajaxRespond(array('data'=>$result));
    }
    
    //下载csv ,根据搜索结果进行下载
    protected function _msg_downloadInfo(){
    	$userName=I('get.userName',0,'trim');
    	$replayUser=I('get.replayUser',0,'trim');
    	$start=I('get.start',0,'trim');
    	$end=I('get.end',0,'trim');
    	$condition=array();
    	if($userName){
    		$condition[]="msg.name='$userName'";
    	}
        if($replayUser){
    		$condition[]="replay.name='$replayUser'";
    	}
    	if($start){
    		$condition[]="msg.update_time>='$start'";
    		if($end){
    			$condition[]="msg.update_time<='$end'";
    		}
    	}
    	$condition=implode(' and ', $condition);
    	//thinkphp3.2 join操作时,两个表中字段名称不能重名,否则只显示这个重名字段的一个值 eg:replay.name需要写成replay.name as replayName
     	$result=M('msg')->field("msg.title,msg.name,msg.content")->join('left join replay on msg.msg_id = replay.msg_id')->where($condition)->select();
     	$header=array();
     	$header[]=array("主题","作者","内容");
     	$excel=array_merge($header,$result);
     	$fileDir="./Public/Data/msgCsvFiles/";
     	if (!file_exists($fileDir)) {
     		mkdir($fileDir);
     	}
     	foreach($excel as $val){
     		// 插入csv
     		$inputStr = implode($val,",");
     		$fileName=$fileDir.date('Ymdhms').'.csv';
     		file_put_contents($fileName,$inputStr."\n",FILE_APPEND);//使用 FILE_APPEND 可避免删除文件中已有的内容。
     	}
     	$fileName="http://".$_SERVER["SERVER_NAME"].__ROOT__.$fileName;
     	$this->ajaxRespond(array('success'=>true,'fileName'=>$fileName));
    	
    }
    
    public function info(){
    	
     	$msgId=I("post.msgId",0,'intval');
    	if($msgId==0){
    		exit;
    	}
    	$result=D('msg')->where("msg_id='$msgId'")->select();
    	$this->assign('result',$result);
    	$this->display("content");
    	exit;
    	
    	//这部分主要是为了做分页
    	$rec=D('msg')->select();
    	$count=count($rec);
    	$page=new \Think\Page($count,3);
    	$show = $page->show();// 分页显示输出
     	$list = D('msg')->limit($page->firstRow.','.$page->listRows)->select();// 进行分页数据查询 注意limit方法的参数要使用page类的属性
//      	$page->setConfig('first','首页');
//      	$page->setConfig('last','尾页');
//      	$page->setConfig('prev','上一页');
//      	$page->setConfig('next', '下一页');
//      	$page->setConfig('theme',' %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %HEADER% 当前%NOW_PAGE%页/共%TOTAL_PAGE%页' );
//      	$show=$page->show();
     	$this->assign('page',$show);
    	$this->assign('rec',$list);
    	$this->display("page");
    }
    
    //编辑留言信息
    protected  function _msg_update(){
    	//print_r($_POST);exit;
    	$msgId=I("post.id",0,'intval');
    	$title=I("post.title",0,'trim');
    	$content=I("post.content",0,'trim');
    	$data['title']=$title;
    	$data['content']=$content;
    	$msg=D('msg');    	
    	$result=$msg->where("msg_id='$msgId'")->save($data);
    }
    
}